<?php

class SearchController extends FrontEndController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view', 'getpeople', 'getgroups', 'getaudios', 'getvideos'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionIndex()
	{
		$tabs = array(
			'groups',
			'audio',
			'video'
		);
		
		$limit = 10;
		
		if(isset($_GET['tab']) && in_array($_GET['tab'], $tabs)){
			$current_tab = $_GET['tab'];
		}else{
			$current_tab = 'people';
		}
		
		if(isset($_GET['text'])){
			$search = $_GET['text'];
		}else{
			$search = '';
		}
				
		if($current_tab == 'groups'){
			$search = addcslashes($search, '%_');
			$data = Groups::model()->findAll(array(
				'order'=> 'name ASC',
				'limit'=>$limit,
				'condition'=> 'name LIKE :search OR description LIKE :search',
				'params'=> array(':search'=>"%$search%")
			));
		}elseif($current_tab == 'audio'){
			$search = addcslashes($search, '%_');
			$data = Music::model()->findAll(array(
				'order'=> 'artist ASC',
				'limit'=>$limit,
				'condition'=> 'name LIKE :search OR artist LIKE :search',
				'params'=> array(':search'=>"%$search%")
			));
		}elseif($current_tab == 'video'){	
			$search = addcslashes($search, '%_');
			$data = VideoUser::model()->findAll(array(
				'order'=> 'name ASC',
				'limit'=>$limit,
				'condition'=> 'name LIKE :search',
				'params'=> array(':search'=>"%$search%")
			));
		}elseif($current_tab == 'people'){	
			$search = addcslashes($search, '%_');
			$data = Users::model()->findAll(array(
				'order'=> 'full_name ASC',
				'limit'=>$limit,
				'condition'=> 'full_name LIKE :search',
				'params'=> array(':search'=>"%$search%")
			));	
		}
	
		$countries = Country::model()->findAll();
		
		$groupTypes = GroupType::model()->findAll();
		
		$this->render('index', array(
			'current_tab'=>$current_tab,
			'search'=>$search,
			'data'=>$data,
			'countries'=>$countries,
			'groupTypes'=>$groupTypes,
			'limit'=>$limit,
			'offset'=>$limit
		));
	}
	
	public function actionGetPeople(){
		if (Yii::app()->request->isAjaxRequest) {
			$criteria = new CDbCriteria();
			$criteria->order = 'full_name ASC';

			$offset = 0;
			
			if(!empty($_GET['offset'])){
				$criteria->offset = $_GET['offset'];
				$offset = $_GET['offset'];
			}
				
			if(!empty($_GET['limit'])){
				$criteria->limit = $_GET['limit'];
				$offset = $offset + $_GET['limit'];
			}			
			
			$condition = '';

			if(!empty($_GET['offset'])){
				$criteria->offset = $_GET['offset'];
			}			
			
			if(!empty($_GET['text'])){
				$match = addcslashes($_GET['text'], '%_');
				$condition = 'full_name LIKE "%' . $_GET['text'] . '%"';
			}
			
			if(!empty($_GET['filter_country'])){
				if($condition){
					$condition .= ' AND country_id="' . $_GET['filter_country'] . '"';
				}else{
					$condition .= 'country_id="' . $_GET['filter_country'] . '"';
				}
			}		

			if(!empty($_GET['filter_age_from'])){
				if($condition){
					$condition .= ' AND birthday <= DATE_SUB(curdate(), INTERVAL ' . $_GET['filter_age_from'] . ' YEAR)';
				}else{
					$condition .= 'birthday <= DATE_SUB(curdate(), INTERVAL ' . $_GET['filter_age_from'] . ' YEAR)';
				}				
			}

			if(!empty($_GET['filter_age_to'])){
				if($condition){
					$condition .= ' AND birthday >= DATE_SUB(curdate(), INTERVAL ' . $_GET['filter_age_to'] . ' YEAR)';
				}else{
					$condition .= 'birthday >= DATE_SUB(curdate(), INTERVAL ' . $_GET['filter_age_to'] . ' YEAR)';
				}
			}		
			
			if(!empty($_GET['filter_sex'])){
				if($condition){
					$condition .= ' AND sex="' . $_GET['filter_sex'] . '"';
				}else{
					$condition .= 'sex="' . $_GET['filter_sex'] . '"';
				}
			}			
			
			$criteria->condition=$condition;
			
			$data = Users::model()->findAll( $criteria );

			$this->renderPartial('_peopleList',array(
				'people' => $data,
				'limit' => (!empty($_GET['limit'])) ? $_GET['limit'] : '',
				'offset' => $offset					
			));
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}		
	}
	
	public function actionGetGroups(){
		if (Yii::app()->request->isAjaxRequest) {
			$criteria = new CDbCriteria();
			$criteria->order = 'name ASC';
			
			$offset = 0;

			if(!empty($_GET['offset'])){
				$criteria->offset = $_GET['offset'];
				$offset = $_GET['offset'];
			}			
			
			if(!empty($_GET['limit'])){
				$criteria->limit = $_GET['limit'];
				$offset = $offset + $_GET['limit'];
			}			
			
			$condition = '';
			
			if(!empty($_GET['text'])){
				$match = addcslashes($_GET['text'], '%_');
				$condition = '(name LIKE "%' . $_GET['text'] . '%" OR description LIKE "%' . $_GET['text'] . '%")';
			}
				
			if(!empty($_GET['filter_country'])){
				if($condition){
					$condition .= ' AND country_id="' . $_GET['filter_country'] . '"';
				}else{
					$condition .= 'country_id="' . $_GET['filter_country'] . '"';
				}
			}
			
			if(!empty($_GET['filter_group_type'])){
				if($condition){
					$condition .= ' AND approve="' . $_GET['filter_group_type'] . '"';
				}else{
					$condition .= 'approve="' . $_GET['filter_group_type'] . '"';
				}
			}			
				
			$criteria->condition=$condition;
				
			$data = Groups::model()->findAll( $criteria );

			$this->renderPartial('_groupList',array(
				'groups' => $data,
				'limit' => (!empty($_GET['limit'])) ? $_GET['limit'] : '',
				'offset' => $offset
			));
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}
	}	
	
	public function actionGetAudios(){
		if (Yii::app()->request->isAjaxRequest) {
			$criteria = new CDbCriteria();
			$criteria->order = 'artist ASC';
				
			$offset = 0;
		
			if(!empty($_GET['offset'])){
				$criteria->offset = $_GET['offset'];
				$offset = $_GET['offset'];
			}
				
			if(!empty($_GET['limit'])){
				$criteria->limit = $_GET['limit'];
				$offset = $offset + $_GET['limit'];
			}
				
			$condition = '';
				
			if(!empty($_GET['text'])){
				$match = addcslashes($_GET['text'], '%_');
				$condition = '(name LIKE "%' . $_GET['text'] . '%" OR artist LIKE "%' . $_GET['text'] . '%")';
			}
		
			$criteria->condition=$condition;
		
			$data = Music::model()->findAll( $criteria );
		
			$this->renderPartial('_audioList',array(
				'audios' => $data,
				'limit' => (!empty($_GET['limit'])) ? $_GET['limit'] : '',
				'offset' => $offset
			));
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}		
	}
	
	public function actionGetVideos(){
		if (Yii::app()->request->isAjaxRequest) {
			$criteria = new CDbCriteria();
			$criteria->order = 'name ASC';
		
			$offset = 0;
		
			if(!empty($_GET['offset'])){
				$criteria->offset = $_GET['offset'];
				$offset = $_GET['offset'];
			}
		
			if(!empty($_GET['limit'])){
				$criteria->limit = $_GET['limit'];
				$offset = $offset + $_GET['limit'];
			}
		
			$condition = '';
		
			if(!empty($_GET['text'])){
				$match = addcslashes($_GET['text'], '%_');
				$condition = 'name LIKE "%' . $_GET['text'] . '%"';
			}
		
			$criteria->condition=$condition;
		
			$data = VideoUser::model()->findAll( $criteria );
		
			$this->renderPartial('_videoList',array(
					'videos' => $data,
					'limit' => (!empty($_GET['limit'])) ? $_GET['limit'] : '',
					'offset' => $offset
			));	
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}		
	}
}
